<template>
  <el-dialog
    :title="!dataForm.courseId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="教师名称" prop="teacherId">
      <!-- <el-input v-model="dataForm.teacherId" placeholder="teacher基本信息id"></el-input> -->
      <el-select v-model="dataForm.teacherId" placeholder="请选择">
    <el-option
      v-for="item in options"
      :key="item.value"
      :label="item.name"
      :value="item.id">
    </el-option>
  </el-select>
    </el-form-item>
    <el-form-item label="课程名称" prop="courseName">
      <el-input v-model="dataForm.courseName" placeholder="课程名称"></el-input>
    </el-form-item>
    <el-form-item label="班级" prop="classr">
      <el-input v-model="dataForm.classr" placeholder="班级"></el-input>
    </el-form-item>
    <el-form-item label="学生人数" prop="studentNumber">
      <el-input v-model="dataForm.studentNumber" placeholder="学生人数"></el-input>
    </el-form-item>
    <el-form-item label="课程开始时间" prop="courseBeginTime">
      <!-- <el-input v-model="dataForm.courseBeginTime" placeholder="课程开始时间"></el-input> -->
      <el-date-picker
      v-model="dataForm.courseBeginTime"
      type="datetime"
      placeholder="选择日期时间">
    </el-date-picker>
    </el-form-item>
    <el-form-item label="课程结束时间" prop="courseEndTime">
      <!-- <el-input v-model="dataForm.courseEndTime" placeholder="课程结束时间"></el-input> -->
      <el-date-picker
      v-model="dataForm.courseEndTime"
      type="datetime"
      placeholder="选择日期时间">
    </el-date-picker>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        options:[],
        visible: false,
        dataForm: {
          courseId: 0,
          teacherId: '',
          courseName: '',
          classr: '',
          studentNumber: '',
          courseBeginTime: '',
          courseEndTime: ''
        },
        dataRule: {
          teacherId: [
            { required: true, message: 'teacher基本信息id不能为空', trigger: 'blur' }
          ],
          courseName: [
            { required: true, message: '课程名称不能为空', trigger: 'blur' }
          ],
          class: [
            { required: true, message: '班级不能为空', trigger: 'blur' }
          ],
          studentNumber: [
            { required: true, message: '学生人数不能为空', trigger: 'blur' }
          ],
          courseBeginTime: [
            { required: true, message: '课程开始时间不能为空', trigger: 'blur' }
          ],
          courseEndTime: [
            { required: true, message: '课程结束时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
   created(){
      this.getteacherInfo()
    },
    methods: {
       getteacherInfo(){

        // 获取教师基本信息
        this.$http({
        url: this.$http.adornUrl('/teacherservice/teachertitle/teacher/info'),
        method: 'get',
        params: this.$http.adornParams({})
        }).then(({data}) => {
          console.log(data)
          this.options=data.data
          console.log("jdvnd",this.options)
        })
      

      },
      init (id) {
        this.dataForm.courseId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.courseId) {
            this.$http({
              url: this.$http.adornUrl(`/teacherservice/teachercourse/info/${this.dataForm.courseId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.teacherId = data.teacherCourse.teacherId
                this.dataForm.courseName = data.teacherCourse.courseName
                this.dataForm.classr = data.teacherCourse.classr
                this.dataForm.studentNumber = data.teacherCourse.studentNumber
                this.dataForm.courseBeginTime = data.teacherCourse.courseBeginTime
                this.dataForm.courseEndTime = data.teacherCourse.courseEndTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/teacherservice/teachercourse/${!this.dataForm.courseId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'courseId': this.dataForm.courseId || undefined,
                'teacherId': this.dataForm.teacherId,
                'courseName': this.dataForm.courseName,
                'classr': this.dataForm.classr,
                'studentNumber': this.dataForm.studentNumber,
                'courseBeginTime': this.dataForm.courseBeginTime,
                'courseEndTime': this.dataForm.courseEndTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
